import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

import '../../../utils/log_util.dart';
import 'course_video_details_controller.dart';

class VideoDetailsLocalController extends GetxController {
  int videoId;
  Duration currentDuration;
  Duration totalDuration;
  int videoCourseId;

  VideoDetailsLocalController(this.videoId);

  void updateProgress(
      Duration currentDuration, Duration totalDuration, int videoCourseId) {
    this.currentDuration = currentDuration;
    this.totalDuration = totalDuration;
    this.videoCourseId = videoCourseId;
  }

  bool isClose = false;

  Future<bool> closePageUpdateProgress() async {
    isClose = true;
    if (_videoPlayerController != null) {
      await _videoPlayerController.pause();
      await _videoPlayerController.dispose();
      _videoPlayerController = null;
    }
    if (currentDuration != null && totalDuration != null) {
      if (Get.isRegistered<CourseVideoDetailsController>(
          tag: "$videoCourseId")) {
        CourseVideoDetailsController controller =
            Get.find<CourseVideoDetailsController>(tag: "$videoCourseId");
        await controller.clickStopVideo(
          videoId: videoId,
          videoCourseId: videoCourseId,
          inMilliseconds: currentDuration.inMilliseconds,
        );
      } else {
        LogUtils.e(
            "页面关闭 closePageUpdateProgress CourseVideoDetailsController is not Registered ");
      }
    } else {
      LogUtils.e("页面关闭 closePageUpdateProgress currentDuration is null ");
    }
    return true;
  }

  VideoPlayerController _videoPlayerController;

  void initVideoController(VideoPlayerController controller) {
    _videoPlayerController = controller;
  }
}
